<?php

    // +----------------------------------------------------------------------
    // | 行为日志控制器
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\ucenter\admin;

    use app\event\ucenter\IpPool as IpPoolEvent;
	use app\model\ucenter\IpPool as IpPoolModel;
	use app\controller\admin\Common;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;

	class IpPool extends Common
    {
        protected function initialize()
        {
            parent::initialize();
        }


        public function _filter()
        {
            $map[] = ['status', '>', -2];
            return $map;
        }


        /**
         * @return mixed|string
         * @author  : 微尘 <yicmf@qq.com>
         */
        public function index()
        {


            // 显示页面
            $builder = new Table();
            $builder->model(IpPoolModel::class)
                ->where($this->_filter())
                ->order('id DESC')
                //                ->suggest('请定期清理日志，提示网站运行性能')
//                ->searchText('remark', '内容', '日志内容')
                ->keyId()
                ->keyIp()
                ->keyText('country', '国家')
                ->keyText('region_name', '地区')
                ->keyText('city', '城市')
//                ->keySwitch('status','是否可以访问')
                ->keyCreateTime()
				->keyStatus()
				->actionUpdate()
				->actionForbid()
				->actionRestore()
			;
            return $builder->fetch();
        }

        /**
         * IP详情
         */
        public function update()
        {
			if ($this->request->isPost()) {
				$event = new IpPoolEvent();
				$result = $event->update($this->request->post(), $this->user);
				if (0 == $result['code']) {
					$this->_returnSuccess( $result['message']);
				} else {
					$this->_returnError( '操作失败，失败信息【' . $result['message'] . '】');
				}
			} else {
				$data = IpPoolModel::where('status', 1)->where('id', $this->request->param('id'))->find();
				if (!$data)
				{
					$this->_returnError( '不存在');
				}
				$builder = new Edit();

				$builder->title('ip编辑')
					->keyReadOnly('ip', 'ip')
					->keyTextInline('country', '国家')
					->keyTextInline('region_name', '地区')
					->keyTextInline('city', '城市')
//					->keySwitch('status', '可以访问')
					->data($data)
					->buttonSubmit();
				return $builder->fetch();
			}
        }
		public function forbid()
		{
			$course = IpPoolModel::where('id', $this->request->param('id'))->find();
			if ($course) {
				$course['status'] = -1;
				if ($course->save()) {
					$this->_returnSuccess('禁用成功');;
				} else {
					$this->_returnError('禁用失败');
				}
			} else {
				$this->_returnError('操作异常');
			}
		}

		public function restore()
		{
			$course = IpPoolModel::where('id', $this->request->param('id'))->find();
			if ($course) {
				$course['status'] = 1;
				if ($course->save()) {
					$this->_returnSuccess('还原成功');;
				} else {
					$this->_returnError('还原失败');
				}
			} else {
				$this->_returnError('操作异常');
			}
		}
    }
